﻿using DependencyInjectionScopeAndDisposableDemo.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DependencyInjectionScopeAndDisposableDemo.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
            "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="orderService"></param>
        /// <param name="orderService1"></param>
        /// <param name="hostApplicationLifetime">IHostApplicationLifetime这个接口是用来管理我们整个应用程序的声明周期的</param>
        /// <param name="stop"></param>
        /// <returns></returns>
        [HttpGet]
        public int Get([FromServices] IHostApplicationLifetime hostApplicationLifetime
            , [FromQuery] bool stop = false)
        {

            if (stop)
            {
                //他可以把我们整个应用程序关掉
                hostApplicationLifetime.StopApplication();
            }

            #region 
            //Console.WriteLine("==========111111111============");
            //using (IServiceScope scope = HttpContext.RequestServices.CreateScope())//RequestServices是指当前请求的一个容器，也就是我们当前请求的一个根容器。是应用程序根容器的一个子容器，每个请求都会创建一个容器，在这里我在这个子容器下面，我们再创建一个子容器来获取我们的服务，
            //{
            //    var service = scope.ServiceProvider.GetService<IOrderService>();
            //    var service1 = scope.ServiceProvider.GetService<IOrderService>();
            //}
            //Console.WriteLine("==========22222222222============");
            #endregion

            //Console.WriteLine("接口请求处理结束");
            return 1;
        }


    }
}
